﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class NhaCungCapDAO
    {
        public List<NhaCungCapDTO> LayDanhSachNhaCungCap()
        {
            List<NhaCungCapDTO> listnhacungcap = new List<NhaCungCapDTO>();
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                String sql = "Select * from NhaCungCap";

                conn = DataProvider.ConnectDB();
                SqlCommand cm = new SqlCommand(sql, conn);
                reader = cm.ExecuteReader();
                while (reader.Read())
                {
                    NhaCungCapDTO nccDTO = new NhaCungCapDTO();
                    nccDTO.MaNCC = reader.GetInt32(0);
                    if (reader[1] != DBNull.Value)
                    {
                        nccDTO.Ten = reader.GetString(1);
                    }
                    if (reader[2] != DBNull.Value)
                    {
                        nccDTO.DiaChi = reader.GetString(2);
                    }
                    if (reader[3] != DBNull.Value)
                    {
                        nccDTO.DienThoai = reader.GetInt32(3);
                    }
                    if (reader[4] != DBNull.Value)
                    {
                        nccDTO.MoTa = reader.GetString(4);
                    }
                    listnhacungcap.Add(nccDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return listnhacungcap;
        }
    }
}
